package view.ui
{
	import control.manager.BitmapManager;
	import control.manager.LayerManager;
	import control.manager.RoleManager;
	import control.manager.TaskManager;
	
	import data.PlayerData;
	import data.TaskVO;
	import data.UserTaskVO;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.filters.GlowFilter;
	import flash.geom.Point;
	import flash.text.StyleSheet;
	import flash.text.TextField;
	
	import flashx.textLayout.formats.TextDecoration;
	
	import ghostcat.display.game.Clock;
	
	import view.item.TaskText;
	import view.layer.GameLayer;
	import view.role.MapMonster;
	import view.role.MapNPC;
	import view.role.MapObject;
	import view.role.MapPlayer;

	/**
	 * 右侧浮动任务栏 
	 * @author leo
	 * 
	 */	
	public class UITaskTrack extends Sprite
	{
		
		private static var _I :UITaskTrack = null;
		/**
		 *  发光滤镜
		 */		
		private const MOUSE_ON :Array = [new GlowFilter(0xffff00,1,4,4,2,2)];
		/**
		 * ui里所有的内容 
		 */		
		private var content :MovieClip;
		
		/**
		 * +- 按钮容器 
		 */		
		private var btn_ShowContent :MovieClip;
		/**
		 * 任务表容器 
		 */		
		private var tab_Task :MovieClip;
		/**
		 * 任务黑色背景 
		 */		
		private var taskContentBG :MovieClip;
		/**
		 * 当前任务按钮容器 
		 */		
		private var tabButton_Doing :MovieClip;
		/**
		 * 当前任务按钮 
		 */		
		private var btn_doing :SimpleButton;
		/**
		 * 当前任务文本 
		 */		
		private var txtDoing :TextField;
		/**
		 * 可接任务按钮容器 
		 */		
		private var tabButton_CanDo :MovieClip;
		/**
		 * 可接任务按钮 
		 */		
		private var btn_canDo :SimpleButton;
		/**
		 * 可接任务文本 
		 */		
		private var txtCanDo :TextField;
		/**
		 * + 显示任务按钮 
		 */		
		private var btn_show :SimpleButton;
		/**
		 * - 隐藏任务按钮 
		 */		
		private var btn_hide :SimpleButton;
		/**
		 * 任务信息的容器 （任务内容文本请加在这里）
		 */		
		private var task_data_content :MovieClip;
		/**
		 * 红色遮罩层 
		 */		
		private var contentMask :MovieClip;
		/**
		 * 下拉条 
		 */		
		private var scrollV :MovieClip;
		/**
		 * 下拉条块 
		 */		
		private var m_ScrollBox :MovieClip;
		/**
		 * 上拉按钮 
		 */		
		private var m_ScrollUp :SimpleButton;
		/**
		 * 下拉按钮 
		 */		
		private var m_ScrollDown :SimpleButton;
		/**
		 * 任务内容文本 
		 */		
		private var txtTask :TextField;
		
		
		private var doingTaskList :Array;
		private var canDoTaskList :Array;
		
		private var txtList :Array = [];
		public function UITaskTrack(prv :PrivateClass)
		{
			super();
			
			var mc :MovieClip = BitmapManager.instance.getMCByName("TaskTrack");
			if(mc)
			{
				this.addChild(mc);
				this.content = mc;
			}else
			{
				return;
			}
			
			this.btn_ShowContent = mc.btn_ShowContent;
			this.tab_Task = mc.tab_Task;
			this.taskContentBG = mc.taskContentBG;
			
			this.btn_hide = btn_ShowContent.btn_hide;
			this.btn_show = btn_ShowContent.btn_show;
			
			this.tabButton_CanDo = tab_Task.tabButton_CanDo;
			this.tabButton_Doing = tab_Task.tabButton_Doing;
			this.task_data_content = tab_Task.task_data_content;
			this.contentMask = task_data_content.contentMask;
			
			
			this.btn_doing = tabButton_Doing.btn_doing;
			this.btn_canDo = tabButton_CanDo.btn_canDo;
			
			this.scrollV = task_data_content.scrollV;
			this.m_ScrollBox = scrollV.m_ScrollBox;
			this.m_ScrollDown = scrollV.m_ScrollDown;
			this.m_ScrollUp = scrollV.m_ScrollUp;
			
			this.btn_show.visible = false;
			this.scrollV.visible = false;
			this.taskContentBG.alpha = 0;
			addEventListener(MouseEvent.CLICK,mouseHandler);
			addEventListener(MouseEvent.MOUSE_OVER,mouseHandler);
			addEventListener(MouseEvent.MOUSE_OUT,mouseHandler);
			
			click(this.btn_canDo);
		}
		
		public function updateTaskTrack():void
		{
		}
		/**
		 * 增加一个任务文本显示 
		 * @param taskVO
		 * 
		 */		
		public function addTask(vo :*,isDoing :Boolean = false):void
		{
			var taskText :TaskText = new TaskText();
			taskText.selectable = false;
			taskText.width = 190;
			if(vo is TaskVO)
			{
				var taskVO :TaskVO = vo as TaskVO;
				taskText.taskID = taskVO.taskID;
				taskText.htmlText ="<u><font color='#fff000'>[主线]</font></u>" + "<a href=\"event:"+taskVO.sendNPCID+"\">"+taskVO.taskName+"</a>";
			}else if(vo is UserTaskVO)
			{
				var userTaskVO :UserTaskVO = vo as UserTaskVO;
				taskText.taskID = userTaskVO.taskID;
				
				//是否可以完成
				var value :Boolean = userTaskVO.canComplete;
				var str :String = "";
				if(value)
				{
					str = "(可完成)";
				}else
				{
					str = "(未完成)";
				}
				//是否是战斗任务
				
				var id :String = "";
				if(userTaskVO.taskVO)
				{
					if(userTaskVO.taskVO.taskSort == TaskVO.BATTLE_SORT)
					{
						if(userTaskVO.canComplete)
						{
							id = userTaskVO.completeID;
						}else
						{
							if(userTaskVO.taskVO.battleTaskData && userTaskVO.taskVO.battleTaskData.length > 0)
							{
								id = userTaskVO.taskVO.battleTaskData[1];
							}
						}
					}else
					{
						id = userTaskVO.completeID;
					}
				}
				taskText.htmlText ="<u><font color='#fff000'>[主线]</font></u>" + 
					"<a href=\"event:"+ id +"\">"+userTaskVO.taskVO.taskName +"</a>" +
					"<u><font color='#fff000'>" + str + "</font></u>";
			}
			
			var styleSheet :StyleSheet = new StyleSheet();
			styleSheet.setStyle("a",{textDecoration : "underline",color : "#ff0000"});
			taskText.styleSheet = styleSheet;
			
			taskText.addEventListener(TextEvent.LINK,linkHandler);
			
			this.task_data_content.addChild(taskText);
			
			this.txtList.push(taskText);
		}
		
		/**
		 * 文本链接 
		 * @param e
		 * 
		 */		
		private function linkHandler(e :TextEvent):void
		{
			trace(e.text);
			
			var id :String = e.text;
			
			if(!id) return;
			if(id.indexOf("npc") != -1)
			{
				var npc :MapNPC = RoleManager.instance.findNPCByID(id);
				if(npc)
				{
					//模拟一次点击npc
					npc.onClick(npc);
				}else
				{
					//如果没有找到NPC，检查下是否需要是其他地图的NPC
					RoleManager.instance.isNeedChangeMapByNPC(id);
				}
			}else if(id.indexOf("monster") != -1)
			{
				var monster :MapMonster = RoleManager.instance.findMonsterByID(id);
				if(monster)
				{
					var player :MapPlayer = RoleManager.instance.player;
					if(player)
					{
						player.onClick(monster);
					}
				}else
				{
					//如果没有找到怪物，检查下是否需要是其他地图的怪物
					RoleManager.instance.isNeedChangeMapByMonster(id);
				}
			}
			
		}
		/**
		 * 删除一个任务文本显示 
		 * @param taskVO
		 * 
		 */		
		public function removeTask(taskVO :TaskVO,isDoing :Boolean = false):void
		{
			if(!this.txtList || this.txtList.length == 0)
			{
				return;
			}
			for (var i :int = this.txtList.length -1; i >= 0; i--)
			{
				var txt :TaskText = this.txtList[i] as TaskText;
				if(txt.taskID == taskVO.taskID)
				{
					txt.removeEventListener(TextEvent.LINK,linkHandler);
					if(txt.parent)
					{
						txt.parent.removeChild(txt);
					}
					
					this.txtList.splice(i,1);
				}
			}
		}
		
		private function mouseHandler(e :MouseEvent):void
		{
			if (e.type == MouseEvent.CLICK)
			{
				switch(e.target)
				{
					case this.btn_show:
						trace("显示任务");
						this.btn_show.visible = false;
						this.btn_hide.visible = true;
						this.task_data_content.visible = true;
						this.taskContentBG.alpha = 1;
						break;
					case this.btn_hide:
						trace("隐藏任务");
						this.btn_hide.visible = false;
						this.btn_show.visible = true;
						this.task_data_content.visible = false;
						this.taskContentBG.alpha = 0;
						break;
					case this.btn_doing :
						trace("当前任务");
						click(this.btn_doing);
						break;
					case this.btn_canDo:
						trace("可接任务");
						click(this.btn_canDo);
						break;
				}
			}else
			{
				if(this.btn_show.visible == true)  return;
				switch(e.type)
				{
					case MouseEvent.MOUSE_OVER:
						this.taskContentBG.alpha = 1;
						this.scrollV.visible = true;
						break;
					case MouseEvent.MOUSE_OUT:
						this.taskContentBG.alpha = 0;
						this.scrollV.visible = false;
						break;
				}
			}
		}
		
		/**
		 * 点击可接任务或正在进行的任务按钮 
		 * @param btn
		 * 
		 */		
		private function click(btn :SimpleButton):void
		{
			switch(btn)
			{
				case this.btn_canDo:
					this.btn_canDo.filters = MOUSE_ON;
					this.btn_doing.filters = null;
					showCanDoTask();
					break;
				case this.btn_doing:
					this.btn_doing.filters = MOUSE_ON;
					this.btn_canDo.filters = null;
					showDoingTask();
					break;
			}
		}
		/**
		 * 显示可接任务 
		 * 
		 */		
		private function showCanDoTask():void
		{
			removeTaskTxt();
			canDoTaskList = TaskManager.instance.findCanAcceptTaskList();
			for each(var taskVO :TaskVO in canDoTaskList)
			{
				addTask(taskVO);
			}
		}
		/**
		 * 显示正在进行的任务 
		 * 
		 */		
		private function showDoingTask():void
		{
			removeTaskTxt();
			var playerData :PlayerData = RoleManager.instance.playerData;
			this.doingTaskList = playerData.taskingList;
			for each(var userTaskVO :UserTaskVO in this.doingTaskList)
			{
				addTask(userTaskVO);
			}
		}
		/**
		 * 删除所有任务显示文本 
		 * 
		 */		
		private function removeTaskTxt():void
		{
			if(this.txtList && this.txtList.length > 0)
			{
				for (var i :int = this.txtList.length -1; i >= 0; i--)
				{
					var txt :TaskText = this.txtList[i] as TaskText;
					txt.removeEventListener(TextEvent.LINK,linkHandler);
					if(txt.parent)
					{
						txt.parent.removeChild(txt);
					}
					
					this.txtList.splice(i,1);
				}
			}
		}
		/**
		 * 完成任务时切换到可接任务
		 */		
		public function completeTask():void
		{
			click(this.btn_canDo);
		}
		/**
		 * 接受任务时切换到正在进行的任务
		 */		
		public function acceptTask():void
		{
			click(this.btn_doing);
		}

		public static function get I():UITaskTrack
		{
			if(_I == null)
			{
				_I = new UITaskTrack(new PrivateClass());
			}
			return _I;
		}

		
	}
}

class PrivateClass{}